package org.jeecg.modules.simulator.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;

import org.jeecg.modules.simulator.jsonot.*;
import org.jeecg.modules.simulator.loader.*;
import org.jeecg.modules.simulator.model.SceneDataType;
import org.jeecg.modules.simulator.model.SceneParam;
import org.jeecg.modules.simulator.model.SceneTimePeriodParam;
import org.jeecg.modules.simulator.rooter.CaculateProject;
import org.jeecg.modules.simulator.service.GodMapService;
import org.jeecg.modules.simulator.util.UnitCache;
import org.jeecg.modules.simulator.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger2.mappers.RequestParameterMapperImpl;

import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @program: simulator-b20240918
 * @description:
 * @author: renzhg
 * @create: 2024-10-04 15:04
 **/

@Api(tags = "simulator示例")
@RestController
@RequestMapping(value = {"/simulator/SimulatorTestController"}, produces = {"application/json; charset=utf-8"})
@Slf4j
public class  SimulatorTestController {
    ExecutorService executorService = Executors.newFixedThreadPool(5);
    @Autowired
    GodMapService godMapService;
    @Autowired
    private RequestParameterMapperImpl requestParameterMapperImpl;


    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @ApiOperation(value = "Fore", notes = "对外服务接口")
    @RequestMapping(value = "/returnCalculate", method = RequestMethod.GET)
    public Result<String> returnCalculate(@RequestParam(name="projectid", required = true) String projectid) {
        Result<String> result = new Result<>();
        result.setSuccess(true);
        result.setResult("计算成功");
        result.setMessage("计算成功");
        result.setCode(200);

        SceneParam sceneParam = new SceneParam();
        String historyStartTime="07:00:00";
        String historyEndTime="09:00:00";
        String historyDate="2024-10-01";
        sceneParam.setSceneDate(historyDate);
        sceneParam.setSceneType(SceneDataType.allDayHistoryData);
        sceneParam.setStartTime(historyStartTime);
        sceneParam.setEndTime(historyEndTime);

//        GodMapService godMapService = new GodMapService();
        CaculateProject project = godMapService.createProject(sceneParam,1000,redisTemplate);


return result;
    }

    @ApiOperation(value = "createSimPeriodTimeProjectByHistory", notes = "创建简单历史片段项目")
    @RequestMapping(value = "/createSimPeriodTimeProjectByHistory",  method = RequestMethod.POST)
    public Result<String> createSimPeriodTimeProjectByHistory(@RequestBody HistorySimpleQueryCondition condition){
        Result<String> result = new Result<>();
        result.setSuccess(true);
        result.setResult("计算成功");
//        result.setMessage("计算成功");
        result.setCode(200);

        SceneParam sceneParam = new SceneParam();
        sceneParam.setSceneDate(condition.getHistoryDate());
//        sceneParam.setSceneType(SceneDataType.allDayHistoryData);
        sceneParam.setSceneType(SceneDataType.timePeriodHistoryData);
        sceneParam.setStartTime(condition.getHistoryStartTime());
        sceneParam.setEndTime(condition.getHistoryEndTime());

        SceneTimePeriodParam sceneTimePeriodParam = new SceneTimePeriodParam();
        sceneTimePeriodParam.setHistoryTimePlanDate("2024-10-01");
        sceneTimePeriodParam.setHistoryAllPersonDate("2024-10-01");
        sceneTimePeriodParam.setHistoryODProbabilityDate("2024-10-01");
        sceneTimePeriodParam.setHistoryPsgDistributionData("2024-10-01");
        sceneParam.setSceneTimePeriodParam(sceneTimePeriodParam);

        CaculateProject project = godMapService.createProject(sceneParam,condition.getStep(),redisTemplate);

//        String sceneCode = godMapService.createProject(sceneParam);
        result.setMessage(project.getSceneCode());

        return result;
    }

    @ApiOperation(value = "createSimProjectByHistory", notes = "创建简单的历史项目")
    @RequestMapping(value = "/createSimProjectByHistory",  method = RequestMethod.POST)
    public Result<String> createSimProjectByHistory(@RequestBody HistorySimpleQueryCondition condition){
        Result<String> result = new Result<>();
        result.setSuccess(true);
        result.setResult("计算成功");
//        result.setMessage("计算成功");

        SceneParam sceneParam = new SceneParam();
        sceneParam.setSceneDate(condition.getHistoryDate());
        sceneParam.setSceneType(SceneDataType.allDayHistoryData);
        sceneParam.setStartTime(condition.getHistoryStartTime());
        sceneParam.setEndTime(condition.getHistoryEndTime());
        SceneTimePeriodParam sceneTimePeriodParam = new SceneTimePeriodParam();
        sceneTimePeriodParam.setHistoryTimePlanDate("2024-10-01");
        sceneTimePeriodParam.setHistoryAllPersonDate("2024-10-01");
        sceneTimePeriodParam.setHistoryODProbabilityDate("2024-10-01");
        sceneTimePeriodParam.setHistoryPsgDistributionData("2024-10-01");
        sceneParam.setSceneTimePeriodParam(sceneTimePeriodParam);
        CaculateProject project = godMapService.createProject(sceneParam,condition.getStep(),redisTemplate);

        UnitCache.sceneControlMap.put(project.getSceneCode(),project);


        result.setMessage(project.getSceneCode());
        result.setCode(200);
        return result;
    }


    @ApiOperation(value = "controlStatusSimpleHistoryProject", notes = "简单历史场景控制条件提交")
    @RequestMapping(value = "/controlStatusSimpleHistoryProject", method = RequestMethod.POST)
    public Result<String> controlStatusSimpleHistoryProject(@RequestBody SimpleHistoryControlCondition condition){

        String controlType = condition.getControlType();
        String  sceneCode=condition.getSceneCode();
        Result<String> result = new Result<>();
        SceneControlStatus sceneControlStatus = UnitCache.sceneControlStatusMap.get(sceneCode);
        SceneControlStatus sceneControlStatus2 = UnitCache.sceneControlStatusMap.get(sceneCode);
        CaculateProject caculateProject = UnitCache.sceneControlMap.get(sceneCode);
        String timePt=sceneControlStatus2.getTimePt();
        if(controlType.equals("pause")){
            sceneControlStatus.setProjectControlStatus(ProjectControlStatus.pause);
            result.setResult("暂停时间:"+timePt);
//            UnitCache.sceneControlStatusMap.put(sceneCode,sceneControlStatus);
        }else if(controlType.equals("resume")){

            sceneControlStatus.setProjectControlStatus(ProjectControlStatus.running);
            caculateProject.resume(sceneCode);
            result.setResult("恢复开始时间:"+timePt);
//            UnitCache.sceneControlStatusMap.put(sceneCode,sceneControlStatus);
        }else if(controlType.equals("stop")){
            sceneControlStatus.setProjectControlStatus(ProjectControlStatus.stop);
            result.setResult("停止时间:"+timePt);

        }
        UnitCache.sceneControlStatusMap.put(sceneCode,sceneControlStatus);

        result.setSuccess(true);
//        result.setResult("计算成功");
        result.setMessage("动作成功");
        result.setCode(200);
        return result;

    }






    @ApiOperation(value = "calculateSpeedController", notes = "简单控制后台计算速度")
    @RequestMapping(value = "/calculateSpeedController", method = RequestMethod.POST)
    public Result<String> calculateSpeedController(@RequestBody SimpleCalculateSpeedControlCondition simpleCalculateSpeedControlCondition){
        String sceneCode = simpleCalculateSpeedControlCondition.getSceneCode();
        int speed = simpleCalculateSpeedControlCondition.getSpeed();

        SceneControlStatus sceneControlStatus2 = UnitCache.sceneControlStatusMap.get(sceneCode);
        sceneControlStatus2.setStep(speed);
        UnitCache.sceneControlStatusMap.put(sceneCode,sceneControlStatus2);

        Result<String> result = new Result<>();
        result.setSuccess(true);
        result.setResult("修改完成");
        result.setMessage("修改完成");
        result.setCode(200);
        return result;
    }

    @ApiOperation(value = "addPerson2OneStation", notes = "简单加人")
    @RequestMapping(value = "/addPerson2OneStation", method = RequestMethod.POST)
    public Result<String> addPerson2OneStation(@RequestBody AddPerson2StationCondition addPerson2StationCondition){
        String personCount = addPerson2StationCondition.getPersonCount();
        String stationName = addPerson2StationCondition.getStationName();
        String timePtStr = addPerson2StationCondition.getTimePt();
        String sceneCode = addPerson2StationCondition.getSceneCode();
        String lineId= addPerson2StationCondition.getLineId();
        StationAddPersonNumData stationAddPersonNumData = new StationAddPersonNumData();
        stationAddPersonNumData.setAddPersonNum(Integer.parseInt(personCount));
        stationAddPersonNumData.setStationName(stationName);
        stationAddPersonNumData.setAddTimePoint(timePtStr);
        stationAddPersonNumData.setLineId(lineId);
        UnitCache.stationAddPersonNumDataMap.put(sceneCode,stationAddPersonNumData);
        Result<String> result = new Result<>();
        result.setSuccess(true);
        result.setResult("提交成功");
        result.setMessage("提交成功");
        result.setCode(200);
        return result;

    }



    @ApiOperation(value = "addTrain2Net", notes = "简单加车")
    @RequestMapping(value = "/addTrain2Net", method = RequestMethod.POST)
    public Result<String> addTrain2Net(@RequestBody  SimpleAddTrainCondition simpleAddTrainCondition){
        String addTrainDir = simpleAddTrainCondition.getIsUp();
        String trainId= simpleAddTrainCondition.getAddTrainId();
        String trainNum= simpleAddTrainCondition.getAddTrainNum();

        String addTrainPtId = simpleAddTrainCondition.getAddTrainPtId();//加车地点id
        String addTrainPtType = simpleAddTrainCondition.getAddTrainPtType();//加车地点类型
        String addTrainArriveTime = simpleAddTrainCondition.getAddTrainArriveTime();//加车时间
        String addTrainDepartTime = simpleAddTrainCondition.getAddTrainDepartTime();//加车时间

        String addTrainSpeed = simpleAddTrainCondition.getAddTrainSpeed();//加车速度
        String sceneCode = simpleAddTrainCondition.getSceneCode();//场景类型
        String lineId = simpleAddTrainCondition.getAddLineId();//线路id

        SimpleAddTrainData simpleAddTrainData = new SimpleAddTrainData();
        if(addTrainDir.equals("上行")){
            simpleAddTrainData.setUp(true);
        }else{
            simpleAddTrainData.setUp(false);
        }


        simpleAddTrainData.setAddTrainId(trainId);
        simpleAddTrainData.setAddId(addTrainPtId);
        simpleAddTrainData.setAddTrainNum(trainNum);
        if(addTrainPtType.equals("车辆段")){
            simpleAddTrainData.setAddTrainPointType(AddTrainPointType.park);
        }else{
            simpleAddTrainData.setAddTrainPointType(AddTrainPointType.parkingLine);
        }
        simpleAddTrainData.setAddTrainArriveTime(addTrainArriveTime);
        simpleAddTrainData.setAddTrainDepartTime(addTrainDepartTime);
        simpleAddTrainData.setAddTrainSpeed(addTrainSpeed);
        simpleAddTrainData.setAddLineId(lineId);
        UnitCache.simpleAddTrainDataMap.put(sceneCode,simpleAddTrainData);

        Result<String> result = new Result<>();
        result.setSuccess(true);
        result.setResult("提交成功");
        result.setMessage("提交成功");
        result.setCode(200);
        return result;

    }



    @ApiOperation(value = "getSceneStationInfo", notes = "获取站点信信息")
    @RequestMapping(value = "/getSceneStationInfo", method = RequestMethod.POST)
    public Result<Map<String,StationStaticInfoVo>> getSceneStationInfo(@RequestBody  String sceneCode){

        Map<String, StationStaticInfoVo> stringStationStaticInfoVoMap = UnitCache.stationStaticInfoVoMap.get(sceneCode);

        Result<Map<String,StationStaticInfoVo>> result = new Result<>();
        result.setSuccess(true);
        result.setResult(stringStationStaticInfoVoMap);
        result.setMessage("提交成功");
        result.setCode(200);
        return result;
    }


    @ApiOperation(value = "getSceneNetInfo", notes = "获取线路信息")
    @RequestMapping(value = "/getSceneNetInfo", method = RequestMethod.POST)
    public Result<LineStaticInfoVo> getSceneNetInfo(@RequestBody  String sceneCode){

        LineStaticInfoVo lineStaticInfoVoMap = UnitCache.lineStaticInfoVoMap.get(sceneCode);

        Result<LineStaticInfoVo> result = new Result<>();
            result.setSuccess(true);
            result.setResult(lineStaticInfoVoMap);
            result.setMessage("提交成功");
            result.setCode(200);
            return result;
    }
    @ApiOperation(value = "getSceneOneStationStaticInfo", notes = "获取场景某一站的信息")
    @RequestMapping(value = "/getSceneOneStationStaticInfo", method = RequestMethod.POST)
    public Result<OneStationStaticInfoVo> getOneStationStaticInfoVo(@RequestBody  OneStationInfoCondition oneStationInfoCondition){
        String stationId = oneStationInfoCondition.getStationId();
        String sceneCode = oneStationInfoCondition.getSceneCode();
        Map<String, StationStaticInfoVo> stringStationStaticInfoVoMap = UnitCache.stationStaticInfoVoMap.get(sceneCode);
         for (Map.Entry<String, StationStaticInfoVo> entry : stringStationStaticInfoVoMap.entrySet()) {
             if (entry.getKey().equals(stationId)) {
                 OneStationStaticInfoVo oneStationStaticInfoVo = new OneStationStaticInfoVo();
                 oneStationStaticInfoVo.setStationId(stationId);
                 oneStationStaticInfoVo.setStationName(entry.getValue().getStationName());
                 oneStationStaticInfoVo.setEegNameSpeedMap(entry.getValue().getInterFaceMap());
                 Result<OneStationStaticInfoVo> result = new Result<>();
                 result.setSuccess(true);
                 result.setResult(oneStationStaticInfoVo);
                 result.setMessage("提交成功");
                 result.setCode(200);
                 return result;
             }
         }

        Result<OneStationStaticInfoVo> result = new Result<>();
        result.setSuccess(false);
        result.setResult(null);
        result.setMessage("查询失败，没有找站点信息");
        result.setCode(200);
        return result;
    }

    @ApiOperation(value = "reviseStationParam", notes = "修改站点参数")
    @RequestMapping(value = "/reviseStationParam", method = RequestMethod.POST)
    public Result<String> reviseStationParamCondition(@RequestBody  ReviseStationParamCondition reviseStationParamCondition){

        String stationId = reviseStationParamCondition.getStationId();
        String sceneCode = reviseStationParamCondition.getSceneCode();
        boolean isOpen = reviseStationParamCondition.isOpen();
        Map<String, Integer> speedMap = reviseStationParamCondition.getSpeedMap();
        StationParamOperate stationParamOperate = new StationParamOperate();
        stationParamOperate.setStationId(stationId);
        stationParamOperate.setOpen(isOpen);
        stationParamOperate.setSpeedMap(speedMap);
        stationParamOperate.setStationName(reviseStationParamCondition.getStationName());
        UnitCache.stationParamOperateMap.put(sceneCode,stationParamOperate);

        Result<String> result = new Result<>();
        result.setSuccess(true);
        result.setResult("修改成功");
        result.setMessage("修改成功");
        result.setCode(200);
        return result;

    }


    @ApiOperation(value = "redisSave", notes = "修改站点参数")
    @RequestMapping(value = "/redisSave", method = RequestMethod.POST)

    public Result<String> redisSave(@RequestBody  String sceneCode) {
//        Map<String, List<RouteStation>> t80RouteHisMap = UnitCache.globalSceneBaseData.getT80RouteHisMap();
//        Map<String, List<T80RoutOD>> t80ODRouteMap=UnitCache.globalSceneBaseData.getT80ODRouteMap();
//        Map<String,RoutePath> routePathMap=new HashMap<>();
//        for(Map.Entry<String,List<RouteStation>> routeStationlistEntry :t80RouteHisMap.entrySet()){
//            List<RouteStation> routeStationList = routeStationlistEntry.getValue();
//            RoutePath routePath = new RoutePath();
//            List<PressRouteStation> pressRouteStationList = new ArrayList<>();
//            for(int i=0;i<routeStationList.size();i++){
//                PressRouteStation routeStation = new PressRouteStation();
//                routeStation.setStationSn(routeStationList.get(i).getStationSn());
//                routeStation.setStationId(routeStationList.get(i).getStationId());
//                routeStation.setTripDrctCd(routeStationList.get(i).getTripDrctCd());
//                pressRouteStationList.add(routeStation);
//            }
//            routePath.setRoutes(pressRouteStationList);
//            routePath.setRouteName(routeStationlistEntry.getKey());
//            routePathMap.put(routeStationlistEntry.getKey(),routePath);
//        }
//
//        Map<String,ODRoutes> odRoutesMap=new HashMap<>();
//
//        for(Map.Entry<String, List<T80RoutOD>> t80entry:t80ODRouteMap.entrySet()){
//            List<T80RoutOD> t80RoutODList = t80entry.getValue();
//            ODRoutes  odRoutes= new ODRoutes();
//            List<RoutePath> routePathList = new ArrayList<>();
//            for(int i=0;i<t80RoutODList.size();i++){
//
//                String routeId = t80RoutODList.get(i).getRouteId();
//                RoutePath routePath = routePathMap.get(routeId);
//                routePathList.add(routePath);
//            }
//            odRoutes.setRoutePathList(routePathList);
//            odRoutes.setODName(t80entry.getKey());
//
//            odRoutesMap.put(t80entry.getKey(),odRoutes);
//        }
//
//        System.out.println(odRoutesMap.size());
//
//        for(Map.Entry<String,ODRoutes> entry:odRoutesMap.entrySet()){
//            Gson gson = new GsonBuilder().setPrettyPrinting().create();
//            String jsonResult = gson.toJson(entry.getValue());
//            redisTemplate.opsForValue().set("Simu_"+entry.getKey(),jsonResult);
//
//        }

//        redisTemplate.opsForValue().set("stringValuetest","tsst");
        String pattern ="Simu_*";

        Set<String> keys = redisTemplate.keys(pattern);
        if(keys!=null){
            System.out.println(keys.size());
        }



        Result<String> result = new Result<>();
        result.setSuccess(true);
        result.setResult("修改成功");
        result.setMessage("修改成功");
        result.setCode(200);
        return result;

    }

}
